package com.tarena.lbs.service.impl;

import com.tarena.lbs.dao.UserDao;
import com.tarena.lbs.entity.User;
import com.tarena.lbs.service.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WebServiceImpl implements WebService {

    @Autowired
    private UserDao userDao;


    @Override
    public List<User> getAllUsers() {
        return userDao.getAll();
    }

    @Override
    public boolean updateUser(User user) {
        user.setUpdateTime(new java.util.Date());
        int result = userDao.update(user);
        return result > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        int result = userDao.delete(id);

        return result > 0;
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.getByUsername(username);
    }

    @Override
    public User getUserById(Long id) {
        return userDao.getById(id);
    }

    // 在WebServiceImpl类中实现新增方法
    @Override
    public boolean addStore(User user) {
        // 设置默认状态为启用（0）
        if (user.getStoreStatus() == null) {
            user.setStoreStatus(0);
        }

        // 设置创建时间和更新时间
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);

        // 调用DAO层插入数据
        int rows = userDao.insert(user);
        return rows > 0;
    }
}